package com.res.simulator.handler;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.res.common.handler.CommonHandler;
import com.res.simulator.model.GenericAPIExecutionModel;
import com.res.simulator.model.SavedAPIModel;
import com.res.simulator.plugins.MessageCommunicationClientPlugin;
import com.res.simulator.transformer.ConsumeTemplateSaveTransformer;
import com.res.simulator.view.ConsumerTemplateSaveView;
import com.res.simulator.view.SaveApiView;
import com.res.system.utils.SimulatorUtilities;
import com.res.system.utils.SimulatorUtilities.ConfigData;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.exception.CustomException;

public class ConsumeAPIHandler implements CommonHandler<ConsumerTemplateSaveView, SavedAPIModel> {

    private static final Logger logger = Logger.getLogger(ConsumeAPIHandler.class);

    public ConsumerTemplateSaveView process(final SavedAPIModel model) throws Exception {

        logger.info("Template ID :" + model.getTemplateId());
        logger.info("API ID :" + model.getApiId());
        logger.info("API name :" + model.getApiName());
        logger.info("API String :" + model.getApiString());

        final ConsumeTemplateSaveTransformer templateSaveTransformer = new ConsumeTemplateSaveTransformer();
        final ApiDefinition apiDefinition = templateSaveTransformer.transformModelToDomian(model);
        
        final ApiDefinition apiDefobj = commit(apiDefinition);

        final ConsumerTemplateSaveView apiView = templateSaveTransformer.transformDomianToView(apiDefobj);

        return apiView;
    }

    /**
     * Commited data to back end and return message will be directed to
     * 
     * @param domainCollection
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private ApiDefinition commit(ApiDefinition apiDefinition) throws Exception {

        final CommunicationMessage message = new CommunicationMessage();

        final String serverAddress = SimulatorUtilities.getConfigData(ConfigData.SERVER_ADDRESS);
        final String port = SimulatorUtilities.getConfigData(ConfigData.SERVER_PORT);

        String executorName = "SaveTemplate";
        logger.info("Message Header==>" + "JOBType:GenericAPIExecution" + "|ID:" + serverAddress + "|Port:" + port + "|Executor:" + executorName);

        message.setMessageHeader("JOBType:GenericAPIExecution|Executor:" + executorName + "|ID:" + serverAddress + "|Port:" + port);
        message.setObject(apiDefinition);

        final MessageCommunicationClientPlugin plugin = new MessageCommunicationClientPlugin();
        final CommunicationMessage returnedMessage = plugin.invoke(message);

         ApiDefinition apiDefnition=null;
        if(returnedMessage.getObject() instanceof CustomException){
            CustomException exception=(CustomException)returnedMessage.getObject();
            String exceptionMessage=exception.getMessage();
            int exceptionCode=exception.getExceptionCode();
          throw new CustomException(exceptionMessage, exceptionCode);
        }else{
            apiDefnition    = (ApiDefinition) returnedMessage.getObject();
            
        }
   

        return apiDefnition;

    }

}
